package com.hieu.adapter;

import java.util.ArrayList;
import com.hieu.bean.Trip;
import com.hieu.tripmanager.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TripAdapter extends ArrayAdapter<Trip> {
	Context context;
	int layoutResourceId;
	ArrayList<Trip> mTripList = new ArrayList<Trip>();

	public TripAdapter(Context context, int layoutResourceId,
			ArrayList<Trip> data) {
		super(context, layoutResourceId, data);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.mTripList = data;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void add(Trip object) {
		// TODO Auto-generated method stub
		super.add(object);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = convertView;
		TripHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new TripHolder();
			holder.img = (ImageView) row.findViewById(R.id.imageView1);
			holder.nameTrip = (TextView) row.findViewById(R.id.nameTrip);
			holder.startDay = (TextView) row.findViewById(R.id.startDay);
			holder.endDay = (TextView) row.findViewById(R.id.endDay);
			holder.txtEndDay = (TextView) row.findViewById(R.id.txtEndDay);
			row.setTag(holder);
		} else {
			holder = (TripHolder) row.getTag();
		}

		Trip trip = mTripList.get(position);
		String pathImg = trip.getImg();
		Log.v("position",
				"" + position + "--" + trip.getIdTrip() + "--" + trip.getImg());
		if (pathImg != null) {
			Bitmap img = BitmapFactory.decodeFile(pathImg);
			if(img != null)	holder.img.setImageBitmap(img);
			else Log.v("img trip", "khong lay duoc hinh anh");
		}
		holder.nameTrip.setText(trip.getNameTrip());
		holder.startDay.setText(trip.getStartDay());
		// if ((trip.getEndDay() != null) &&
		// !trip.getEndDay().trim().equals(""))
		holder.endDay.setText(trip.getEndDay());
		// else {
		// holder.txtEndDay.setVisibility(View.INVISIBLE);
		// holder.endDay.setVisibility(View.INVISIBLE);
		// }

		return row;
	}

	static class TripHolder {
		ImageView img;
		TextView nameTrip;
		TextView startDay;
		TextView endDay;
		TextView txtEndDay;
	}

}
